<?php

namespace App\Admin\Controllers\Partner;

use App\Admin\Forms\Partner\PartnerMiningForm;
use App\Admin\Actions\Partner\PartnerMiningCountAction;
use App\Admin\Repositories\Partner\PartnerMiningCount;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Modal;


class PartnerMiningCountController extends AdminController
{

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PartnerMiningCount(["user",'userProduct','partner']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id',"用户号")->link(function ($value){
                return admin_url("/user?id=".$value);
            });;
            $grid->column('user.username',"用户联系方式");
            $grid->column('order_id','订单号')->link(function($value){
                return admin_url("/order/".$value);
            });
            $grid->column('userProduct.product_title','订单名称');
            $grid->column('userProduct.buy_shares','购买数量');
            $grid->column('partner.name','合伙人等级名称');
            $grid->column('total_profit',"分配额度");
            $grid->column('coin',"分配币种");
            $grid->column('date',"出矿日期");
            $grid->column('sub_id',"子用户id");
            $grid->column('test',"备注信息");
            //异步加载信息
            $grid->column('status',"最终审核状态")->using([0 => "未审核", 1 => "已审核", 2 => "已驳回"])->badge([
                0 => 'primary',
                1 => 'success',
                2 => 'danger'
            ]);

            $grid->column('created_at',"创建时间");
            $grid->model()->orderBy("id",'desc');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('user_id','会员号');
                $filter->equal('sub_id','子会员号');
                $filter->like('user.username','会员名称');
                $filter->equal('order_id','订单号');
                $filter->like("userProduct.product_title",'订单名称');
                $filter->like("partner.name",'合伙人等级名称');
                $filter->equal("status","最终结算状态")->select(["未审核","已审核","已驳回"]);
                $filter->between('created_at', '创建时间')->datetime();
            });
            $grid->actions(function (Grid\Displayers\Actions $action){

                if(Admin::user()->can("/partner/partner_mining/delete")) {
                    $action->append(Modal::make()
                        ->lg()
                        ->title("最终审核")
                        ->body(PartnerMiningForm::make()->payload(['id'=>$this->id,'type'=>"status"]))
                        ->button("最终审核"));
                }
            });

            $grid->batchActions([
                new PartnerMiningCountAction('结算', 1),
            ]);

            $titles = [
                "user_id" => "会员号",
                "username" => "会员名称",
                "partner_id" => "合伙人号",
                "partner_name" => "合伙人等级名称",
                "order_id" => "订单号",
                "product_title" => "订单名称",
                "total_profit" => "分配的额度",
                "coin" => "收益币种",
                "status" => "最终审核状态",
                "test" => "备注信息",
                'date' => "出矿日期",
                "created_at" => "创建时间"
            ];

            $grid->export($titles)->filename("合伙人分币收益")->rows(function ($row){
                foreach ($row as $key=>$value){
                    $row[$key]["username"] = $value["user"]['username'];
                    $row[$key]["partner_name"] = $value["partner"]["name"];
                    $row[$key]["product_title"] = $value["user_product"]['product_title'];
                    switch ($value['status']){
                        case 0:
                            $row[$key]['status'] = "未审核";
                            break;

                        case 1:
                            $row[$key]['status'] = "已通过";
                            break;

                        case 2:
                            $row[$key]['status'] = "已驳回";
                            break;

                        default;
                    }
                }
                return $row;
            })->disableExportAll();

            $grid->perPages([5,10,20,50,100,200,500,1000]);
            $grid->disableEditButton();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PartnerMiningCount(["user",'userProduct','partner']), function (Show $show) {
            $show->field('id');
            $show->field('user_id','会员号');
            $show->field('user.username','会员联系方式');
            $show->field('partner_id','合伙人号');
            $show->field('partner.name','合伙人等级名称');
            $show->field('order_id','订单号');
            $show->field('user_product.product_title','订单名称');
            $show->field('coin','币种');
            $show->field('date','出矿日期');
            $show->field('test','备注信息');
            $show->field('primary_status','初级审核状态')->as(function ($value){
                switch ($value){
                    case 0:
                        return "未审核";
                        break;

                    case 1:
                        return "已通过";
                        break;

                    case 2:
                        return "已驳回";
                        break;

                    default;
                }
            });

            $show->field('status','最终审核状态')->as(function ($value){
                switch ($value){
                    case 0:
                        return "未审核";
                        break;

                    case 1:
                        return "已通过";
                        break;

                    case 2:
                        return "已驳回";
                        break;

                    default;
                }
            });

            $show->field('sub_id');
            $show->field('total_profit');
            $show->field('created_at',"创建时间");
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PartnerMiningCount(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('partner_id');
            $form->text('order_id');
            $form->text('coin');
            $form->text('date');
            $form->text('test');
            $form->text('status');
            $form->text('sub_id');
            $form->text('total_profit');
            $form->text('created_at');
        });
    }
}
